const QiniuU = require("../lib").QiniuUniversally;
const path = require("path");
const filesize = require("filesize");



let q = new QiniuU;
QiniuU.__debug.TokenGetter.get_hiaita().then(async token_ => {
  (async $ => {
    q.mkblk_4_file(path.join(__dirname, "../tmp/4.74MB.gif")).then(async o => {
      let o2 = ctxTimesX(o.data, 100);
      console.log(o2.file_size_str);
      // debugger
      let o_hlj = await QiniuU.__debug.FileMakers.mkfile_owhat({
        path: "xxx/aaa/www.bin",
        data: o2.new_ctx_data
      }, 100 * 1024 * 1024);
      debugger
    });

  })();

});



/**
 * @returns {{new_ctx_data:Array<{ctx:String,chunk_size:Number}>,file_size_str:String}}
 * @param {Array<{ctx:String,chunk_size:Number}>} ctx_datas 
 * @param {Number} x 
 */
function ctxTimesX(ctx_datas, x = 500) {
  x = parseInt(x);
  if (x < 1) x = 1;
  let narr_withoutend = ctx_datas.slice(0, ctx_datas.length - 1);
  let narr = ctx_datas.slice(0, ctx_datas.length);
  for (let i = 1; i <= x - 1; i++) {
    narr = narr.concat(narr_withoutend);
  }
  // narr.push(ctx_datas[ctx_datas.length - 1])
  let sorted = Array.from(narr).sort((a, b) => b.chunk_size - a.chunk_size);
  let fs_str = filesize(sorted.map(e => e.chunk_size).reduce((a, b) => a + b))
  // debugger
  return {
    new_ctx_data: sorted,
    file_size_str: fs_str
  }

}